<?php
/**
 * @api {post} http://aicomm.bndvip.com/Api/Login/login/ 账号密码登陆
 * @apiName login
 * @apiGroup Login
 *
 * @apiParam {String} name 账号(必填).
 * @apiParam {String} password 账号密码(必填).
 * @apiParam {String} registrationID 设备id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {post} http://aicomm.bndvip.com/Api/Login/faceLogin/ 人脸登陆
 * @apiName faceLogin
 * @apiGroup Login
 *
 * @apiParam {String} img     img图片转码base64格式(必填).
 * @apiParam {String} name 账号(必填).
 * @apiParam {String} registrationID 设备id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {post} http://aicomm.bndvip.com/Api/Login/DxLogin/ 短信登陆
 * @apiName DxLogin
 * @apiGroup Login
 *
 * @apiParam {String} phone 手机号码(必填).
 * @apiParam {String} dxcode 手机验证码(必填).
 * @apiParam {String} registrationID 设备id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {post} http://aicomm.bndvip.com/Api/Login/register/ 账号注册
 * @apiName register
 * @apiGroup Login
 *
 * @apiParam {String} phone 手机号码(必填).
 * @apiParam {String} dxcode 手机验证码(必填).
 * @apiParam {String} password 密码 大小写 不小于6位(必填).
 * @apiParam {String} checkpassword 确认密码 大小写 不小于6位(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {post} http://aicomm.bndvip.com/Api/Login/getPwd/ 找回密码
 * @apiName getPwd
 * @apiGroup Login
 *
 * @apiParam {String} phone 手机号码(必填).
 * @apiParam {String} dxcode 手机验证码(必填).
 * @apiParam {String} password 密码 大小写 不小于6位(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {post} http://aicomm.bndvip.com/Api/Login/dxSendCode/ 短信发送code
 * @apiName dxSendCode
 * @apiGroup Login
 *
 * @apiParam {Number} phone 手机号码(必填).
 * @apiParam {Number} type 1登陆  2注册  3找回密码 4修改手机号码.
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {post} http://aicomm.bndvip.com/Api/Login/restPwd/ 修改密码
 * @apiName restPwd
 * @apiGroup Login
 *
 * @apiParam {Number} user_id 用户id(必填).
 * @apiParam {String} password 密码
 * @apiParam {String} checkpassword 确认密码
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
namespace app\api\controller;
use app\common\controller\Api;
use \think\Log;
use think\Db;
use think\Request;
use think\Model;
use think\Session;

class Login extends Api{
    //账号登陆
    public function login(){
//                $result['password'] = 'Zj15565';
//                $result['name'] = '15827024197';
        //确认为post
//        if(Request::instance()->isPost()) {
        $result = $this->request->param();
        Log::login('【Login/login】-[result]--'.json_encode($result));
        //账号
        if (empty($result['name'])){
            $return = json_encode(codemsg(-4001));
            Log::login('【Login/login】-[return]--'.json_encode($return));
            echo $return;die;
        }
        if (empty($result['password'])){
            $return = json_encode(codemsg(-4002));
            Log::login('【Login/login】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //数据为空
        //用户名正则 用户名由2-16位数字或字母、汉字、下划线组成！
        $checkname = regcheck('phone',$result['name']);
        if ($checkname){
            Log::login('【Login/login】-[return]--'.json_encode($checkname));
            echo json_encode($checkname);die;
        }
        //密码正则
        $checkpassword = regcheck('password',$result['password']);
        if ($checkpassword){
            Log::login('【Login/login】-[return]--'.json_encode($checkpassword));
            echo json_encode($checkpassword);die;
        }
        //查询用户
        $data['name'] = $result['name'];
        $finduser = model('User')->findUser($data);
        if(empty($finduser)){
            //查询无该用户
            $return = json_encode(codemsg(-4003));
            Log::login('【Login/login】-[return]--'.json_encode($return));
            echo $return;die;
        }
        // 查询用户账号密码匹配记录
        $data['password'] = $this->pwdRet($finduser['password'],$result['password']);
        $findpwd = model('User')->findUser($data);
        if (empty($findpwd)){
            //密码错误
            $return = json_encode(codemsg(-4004));
            Log::login('【Login/login】-[return]--'.json_encode($return));
            echo $return;die;
        }else{
            //查询用户是否存在人脸
            $whereFace['phone'] = $finduser['number'];
            $whereFace['uid'] = $finduser['id'];
            $whereFace['status'] = 1;
            $face = M('face_openid')->where($whereFace)->find();
            if ($face){
                $finduser['faceflag'] = 1;
            }else{
                $finduser['faceflag'] = 0;
            }
            //存session
            $this->sessionUpdate($findpwd['number'],$findpwd['user_id'],$findpwd['nickname']);
            //登陆日志
            $this->loginLog($findpwd['number'],$findpwd['user_id'],0);
            //存设备id
            if ($result['registrationID']){
                $this->jgidupadd($findpwd['user_id'],$result['registrationID']);
            }
            //校正账号密码正确
            $return = codemsg(4005);
            $return['data'] = $finduser;
            $return = json_encode($return);
            Log::login('【Login/login】-[return]--'.json_encode($return));
            echo $return;die;
        }
//        }else{
//            //非post提交
//            $return = json_encode(codemsg(-7));
//            Log::login('【Login/login】-[return]--'.json_encode($return));
//            echo $return;die;
//        }
    }

    //人脸登陆
    public function faceLogin(){
//        if($this->request->isPOST) {
        $result = $this->request->param();
//        $result['uid'] = 13;
//            $result['name'] = 15827024197;
//            $face = new Face();
//            $result['img'] = $face->base64('http://imgbdb3.bendibao.com/shanghai/20152/4/201524103623754.jpg');

        Log::login('【Login/faceLogin】-[result]--'.json_encode($result));
        if (empty($result['name'])){
            //账号不能为空
            $return = json_encode(codemsg(-4001));
            Log::login('【Login/faceLogin】-[return]--'.json_encode($return));
            echo $return;die;
        }
        if (empty($result['img'])){
            //图片不能为空
            $return = json_encode(codemsg(-1));
            Log::login('【Login/faceLogin】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //查询该用户是否存在
        $data['phone'] = $result['name'];
        $finduser = model('User')->findUser($data);
        if (empty($finduser)){
            //该用户不存在
            $return = json_encode(codemsg(-4003));
            Log::login('【Login/faceLogin】-[return]--'.json_encode($return));
            echo $return;die;
        }
        $result['userid'] = $finduser['user_id'];
        $FaceApi = new Face();
        $returnFace = $FaceApi->Face('Phonefind',$result);
        if ($returnFace == 1){
            $this->sessionUpdate($finduser['number'],$finduser['user_id'],$finduser['nickname']);
            //登陆日志
            $this->loginLog($finduser['number'],$finduser['user_id'],1);
            //登陆成功
            //存设备id
            if ($result['registrationID']){
                $this->jgidupadd($finduser['user_id'],$result['registrationID']);
            }
            $return = codemsg(4005);
            $finduser['faceflag'] = 1;
            $return['data'] = $finduser;
            $return = json_encode($return);
            Log::login('【Login/faceLogin】-[return]--'.json_encode($return));
            echo $return;die;
        }else{
            //登陆失败
            $return = json_encode(codemsg(-4006));
            Log::login('【Login/faceLogin】-[return]--'.json_encode($return));
            echo $return;die;
        }

//        }else{
//            //非post提交
//            $return = json_encode(codemsg(-7));
//            Log::login('【Login/faceLogin】-[return]--'.json_encode($return));
//            echo $return;die;
//        }
    }

    //短信登陆
    public function DxLogin(){
//        if($this->request->isPOST) {
        $result = $this->request->param();
        Log::login('【Login/DxLogin】-[result]--'.json_encode($result));
        //账号
        if (empty($result['phone'])){
            $return = json_encode(codemsg(-4001));
            Log::login('【Login/DxLogin】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //手机验证码
        if (empty($result['dxcode'])){
            $return = json_encode(codemsg(-4001));
            Log::login('【Login/DxLogin】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //正则验证
        //账号--手机号码
        $checkphone = regcheck('phone',$result['phone']);
        if ($checkphone){
            $return = $checkphone;
            Log::login('【Login/DxLogin】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //短信验证码
        $checkdxcode = regcheck('dxcode',$result['dxcode']);
        if ($checkdxcode){
            $return = $checkphone;
            Log::login('【Login/DxLogin】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //查询该用户是否存在
        $data['phone'] = $result['phone'];
        $finduser = model('User')->findUser($data);
        if (empty($finduser)){
            //该用户不存在
            $return = json_encode(codemsg(-4003));
            Log::login('【Login/DxLogin】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //短信验证码校验
        $findDx['phone'] = $result['phone'];
        $findDx['type'] = 1;
        $findDx['code'] = $result['dxcode'];
        $sendlog = model('DxSendCode')->findDxList($findDx);
        if (!empty($sendlog)){
            $whereFace['phone'] = $finduser['number'];
            $whereFace['uid'] = $finduser['id'];
            $whereFace['status'] = 1;
            $face = M('face_openid')->where($whereFace)->find();
            if ($face){
                $finduser['faceflag'] = 1;
            }else{
                $finduser['faceflag'] = 0;
            }
            //短信校验成功
            //更改code
            $senddata['code'] = $findDx['code'];
            $senddata['type'] = $findDx['type'];
            $senddata['phone'] = $findDx['phone'];
            $senddata['sendtime'] = $sendlog['sendtime'];
            $senddata['check'] = 1;
            $sendupdata = model('DxSendCode')->updateDx($senddata);
            $this->sessionUpdate($finduser['number'],$finduser['user_id'],$finduser['nickname']);
            //登陆日志
            $this->loginLog($finduser['number'],$finduser['user_id'],1);
            //存设备id
            if ($result['registrationID']){
                $this->jgidupadd($finduser['user_id'],$result['registrationID']);
            }
            $return = codemsg(4005);
            $return['data'] = $finduser;
            $return = json_encode($return);
            Log::login('【Login/DxLogin】-[return]--'.json_encode($return));
            echo $return;die;
        }else{
            //短信校验失败
            $return = json_encode(codemsg(-4007));
            Log::login('【Login/DxLogin】-[result]--'.json_encode($return));
            echo $return;die;
        }
//        }else{
//            $return = json_encode(codemsg(-7));
//            Log::login('【Login/DxLogin】-[return]--'.json_encode($return));
//            echo $return;die;
//        }
    }

    //账号注册
    public function register()
    {
        //确认为post
//        if ($this->request->isPOST) {
        $result = $this->request->param();
//        $result['phone'] = 15827024197;
//        $result['dxcode'] = 668130;
//        $result['password'] = 'Zj15565';
//        $result['checkpassword'] = 'Zj15565';
        Log::login('【Login/register】-[result]--'.json_encode($result));
        if (empty($result['phone'])){
            //手机号码不能为空
            $return = json_encode(codemsg(-1009));
            Log::login('【Login/register】-[return]--'.json_encode($return));
            echo $return;die;
        }
        if (empty($result['dxcode'])){
            //短信验证码不能为空
            $return = json_encode(codemsg(-1012));
            Log::login('【Login/register】-[return]--'.json_encode($return));
            echo $return;die;
        }
        if (empty($result['password'])){
            //密码不能为空
            $return = json_encode(codemsg(-1013));
            Log::login('【Login/register】-[return]--'.json_encode($return));
            echo $return;die;
        }
        if (empty($result['checkpassword'])){
            //确认密码不能为空
            $return = json_encode(codemsg(-1014));
            Log::login('【Login/register】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //正则验证
        //账号--手机号码
        $checkphone = regcheck('phone',$result['phone']);
        if ($checkphone){
            $return = $checkphone;
            Log::login('【Login/register】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //短信验证码
        $checkdxcode = regcheck('dxcode',$result['dxcode']);
        if ($checkdxcode){
            $return = $checkdxcode;
            Log::login('【Login/register】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //密码验证
        $checkpassword = regcheck('password',$result['password']);
        if ($checkpassword){
            $return = json_encode($checkpassword);
            Log::login('【Login/register】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //两次密码是否一致
        if ($result['password'] != $result['checkpassword']){
            //两次密码不一致
            $return = json_encode(codemsg(-1015));
            Log::login('【Login/register】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //查询该手机号是否被注册
        $data['phone'] = $result['phone'];
        $finduser = model('User')->findUser($data);
        if ($finduser){
            //该手机号码已经存在
            $return = json_encode(codemsg(-1016));
            Log::login('【Login/register】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //校对验证码是否正确
        $findDx['phone'] = $result['phone'];
        $findDx['type'] = 2;
        $findDx['code'] = $result['dxcode'];
        $sendlog = model('DxSendCode')->findDxList($findDx);
        if ($sendlog){
            //存在code 进行对比
            if ($sendlog['code'] != $findDx['code']){
                //两次code不一致
                $return = json_encode(codemsg(-3001));
                Log::login('【Login/register】-[return]--'.json_encode($return));
                echo $return;die;
            }
            //验证码确认完毕创建用户
            $user['number'] = $result['phone'];
            $user['mobile'] = $result['phone'];
            $user['reg_time'] = time();//注册时间
            $user['last_login'] = time();//最后登录时间
            $user['mobile_validated'] = 1;//是否手机验证
            $user['last_ip'] = $this->request->ip();//用户ip
            $user['password'] = $this->pwdToSql($result['password']);//用户密码加密
            $userinsert = model('User')->insert($user);
            if (empty($userinsert)){
                //添加失败
                $return = json_encode(codemsg(-3003));
                Log::login('【Login/register】-[return]--'.json_encode($return));
                echo $return;die;

            }else{
                //添加成功
                //更改code
                $senddata['code'] = $findDx['code'];
                $senddata['type'] = $findDx['type'];
                $senddata['phone'] = $findDx['phone'];
                $senddata['sendtime'] = $sendlog['sendtime'];
                $senddata['check'] = 1;
                $sendupdata = model('DxSendCode')->updateDx($senddata);
                //新增用户成功
                $return = json_encode(codemsg(3002));
                Log::login('【Login/register】-[return]--'.json_encode($return));
                echo $return;die;

            }
        }else{
            //请发送手机验证码
            $return = json_encode(codemsg(-3004));
            Log::login('【Login/register】-[return]--'.json_encode($return));
            echo $return;die;
        }

//        }else{
//            $return = json_encode(codemsg(-7));
//            Log::login('【Login/register】-[return]--'.json_encode($return));
//            echo $return;die;
//        }
    }

    //找回密码
    public function getPwd(){
//        if($this->request->isPOST) {
        $result = $this->request->param();
        Log::login('【Login/getPwd】-[result]--'.json_encode($result));
        //账号
        if (empty($result['phone'])) {
            $return = json_encode(codemsg(-4001));
            Log::login('【Login/getPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //手机验证码
        if (empty($result['dxcode'])) {
            $return = json_encode(codemsg(-1012));
            Log::login('【Login/getPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //密码
        if (empty($result['password'])) {
            $return = json_encode(codemsg(-4002));
            Log::login('【Login/getPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //正则验证
        //账号--手机号码
        $checkphone = regcheck('phone', $result['phone']);
        if ($checkphone) {
            $return = $checkphone;
            Log::login('【Login/getPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //短信验证码
        $checkdxcode = regcheck('dxcode', $result['dxcode']);
        if ($checkdxcode) {
            $return = $checkdxcode;
            Log::login('【Login/getPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //密码正则
        $checkpassword = regcheck('password',$result['password']);
        if ($checkpassword){
            $return = $checkpassword;
            Log::login('【Login/getPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //查询该用户是否存在
        $data['phone'] = $result['phone'];
        $finduser = model('User')->findUser($data);
        if (empty($finduser)){
            //该用户不存在
            $return = json_encode(codemsg(-4003));
            Log::login('【Login/getPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //短信验证码校验
        $findDx['phone'] = $result['phone'];
        $findDx['type'] = 3;
        $findDx['code'] = $result['dxcode'];
        $sendlog = model('DxSendCode')->findDxList($findDx);
        if (!empty($sendlog)){
            //启动事物操作
//                Db::startTrans();
//                try{
            //短信校验成功
            //更改code
            $senddata['code'] = $findDx['code'];
            $senddata['type'] = $findDx['type'];
            $senddata['phone'] = $findDx['phone'];
            $senddata['sendtime'] = $sendlog['sendtime'];
            $senddata['check'] = 1;
            $sendupdata = model('DxSendCode')->updateDx($senddata);
            //更改密码
            $updatapwd['phone'] = $findDx['phone'];
            $updatapwd['password'] = $this->pwdToSql($result['password']);//用户密码加密
            $repwd = model('User')->updatapwd($updatapwd);
            if (!empty($repwd)){
                //更改成功
                $return = json_encode(codemsg(4009));
                Log::login('【Login/getPwd】-[return]--'.json_encode($return));
                echo $return;die;
            }else{
                //更改失败
                $return = json_encode(codemsg(-4008));
                Log::login('【Login/getPwd】-[return]--'.json_encode($return));
                echo $return;die;
            }
//                } catch (\Exception $e) {
//                    // 回滚事务
//                    Db::rollback();
//                    //捕获异常记录日志
//                    //返回
//                    $return = json_encode(codemsg(-4008));
//                    Log::login('【Login/getPwd】-[return]--'.json_encode($return));
//                    echo $return;die;
//                }
        }else{
            //短信校验失败
            $return = json_encode(codemsg(-4007));
            Log::login('【Login/getPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }
//        }else{
//            $return = json_encode(codemsg(-7));
//            Log::login('【Login/getPwd】-[return]--'.json_encode($return));
//            echo $return;die;
//        }
    }

    //更改密码
    public function restPwd(){
        $pwd = I('post.password');
        $checkpwd = I('post.checkpassword');
        $userid = I('post.user_id');
        if (empty($userid)){
            //密码不能为空
            $return = json_encode(codemsg(-9));
            Log::login('【Login/restPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //密码
        if (empty($pwd)){
            //密码不能为空
            $return = json_encode(codemsg(-1013));
            Log::login('【Login/restPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }
        if (empty($checkpwd)){
            //确认密码不能为空
            $return = json_encode(codemsg(-1014));
            Log::login('【Login/restPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //两次密码是否一致
        if ($pwd != $checkpwd){
            //两次密码不一致
            $return = json_encode(codemsg(-1015));
            Log::login('【Login/restPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //密码正则
        $checkpassword = regcheck('password',$pwd);
        if ($checkpassword){
            $return = $checkpassword;
            Log::login('【Login/restPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //密码加密
        $save['password'] = $this->pwdToSql($pwd);//用户密码加密
        //修改密码
        $where['user_id'] = array('eq',$userid);
        $data = M('users')->where($where)->update($save);
        if ($data){
            $return = json_encode(codemsg(5004));
            Log::login('【Login/restPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }else{
            $return = json_encode(codemsg(-7021));
            Log::login('【Login/restPwd】-[return]--'.json_encode($return));
            echo $return;die;
        }
    }

    //密码加密
    private function pwdToSql($pwd){
        $salt = '$1$' . \custom\Util::generate_random_lowerletter_number(8) . '$';
        return crypt($pwd, $salt);
    }

    //密码解密 $pwd 数据库查询密码  $postpwd post传送密码
    private function pwdRet($pwd,$postpwd){
        $index           = strrpos($pwd, '$');
        $salt            = substr($pwd, 0, $index+1);
        $pwdcry = crypt($postpwd, $salt);
        return substr($pwdcry,0,strlen($pwdcry)-2);
    }

    //短信发送code
    public function dxSendCode(){
        //确认为post
//        if ($this->request->isPOST) {
        $result = $this->request->param();
//        $result['phone']=15827024197;
//        $result['type']=2;
        Log::login('【Login/dxSendCode】-[result]--'.json_encode($result));
        if (empty($result['phone'])){
            $return = json_encode(codemsg(-1009));
            Log::login('【Login/dxSendCode】-[return]--'.json_encode($return));
            echo $return;die;
        }
        if ($result['type'] != 1 && $result['type'] != 2 && $result['type'] != 3 && $result['type'] != 4){
            //短信发送类型错误
            $return = json_encode(codemsg(-1009));
            Log::login('【Login/dxSendCode】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //账号--手机号码
        $checkphone = regcheck('phone',$result['phone']);
        if ($checkphone){
            $return = $checkphone;
            Log::login('【Login/dxSendCode】-[return]--'.json_encode($return));
            echo $return;die;
        }
        if ($result['type'] == 2 || $result['type'] == 3 || $result['type'] == 4){
            //如果为注册，查看该用户手机号是否已经注册
            $data['phone'] = $result['phone'];
            $finduser = model('User')->findUser($data);

            if (!empty($finduser) && $result['type'] == 2){
                $return = json_encode(codemsg(-1016));
                Log::login('【Login/dxSendCode】-[return]--'.json_encode($return));
                echo $return;die;
            }
            if (!empty($finduser) && $result['type'] == 4){
                $return = json_encode(codemsg(-1016));
                Log::login('【Login/dxSendCode】-[return]--'.json_encode($return));
                echo $return;die;
            }
            if (empty($finduser) && $result['type'] == 3){
                $return = json_encode(codemsg(-4011));
                Log::login('【Login/dxSendCode】-[return]--'.json_encode($return));
                echo $return;die;
            }
        }
        //查询该手机号码发送记录
        $sendlog = model('DxSendCode')->findDxList($result);
        if (empty($sendlog)){
            //发送短信
            $result['code'] = rand(100000,999999);
            $sendDx = model('DxSendCode')->sendDx($result);
            if ($sendDx){
                $sms = sendDx($result['code'],$result['phone'],$result['type']);
                //发送成功
                $return = json_encode(codemsg(1000));
                Log::login('【Login/dxSendCode】-[return]--'.json_encode($return));
                echo $return;die;
            }else{
                //发送失败
                $return = json_encode(codemsg(-1010));
                Log::login('【Login/dxSendCode】-[return]--'.json_encode($return));
                echo $return;die;
            }
        }else{
            //是否过完1分钟
            if (($sendlog['sendtime']+60)>time()){
                //尚未过完一分钟 不能发送
                $return = json_encode(codemsg(-1001));
                Log::login('【Login/dxSendCode】-[return]--'.json_encode($return));
                echo $return;die;
            }else{
                //发送短信
                $data['code'] = $sendlog['code'];
                $data['sendtime'] = $sendlog['sendtime'];
                $data['type'] = $result['type'];
                $data['phone'] = $result['phone'];
                $data['check'] = 0;
                $sendDx = model('DxSendCode')->updateDx($data);
                if ($sendDx){
                    $sms = sendDx($data['code'],$result['phone'],$data['type']);
                    //发送成功
                    $return = json_encode(codemsg(1001));
                    Log::login('【Login/dxSendCode】-[return]--'.json_encode($return));
                    echo $return;die;
                }else{
                    //发送失败
                    $return = json_encode(codemsg(-1010));
                    Log::login('【Login/dxSendCode】-[return]--'.json_encode($return));
                    echo $return;die;
                }
            }
        }
//        }else{
//            $return = json_encode(codemsg(-7));
//            Log::login('【Login/dxSendCode】-[return]--'.json_encode($return));
//            echo $return;die;
//        }
    }

    //登陆成功，添加log
    public function loginLog($phone,$uid,$type){
//        $phone = '15827024197';
//        $uid = 8;
//        $type = 1;
        $data = array();
        $data['ip'] = $this->request->ip();//用户ip
        $data['phone'] = $phone;
        $data['type'] = $type;
        $data['uid'] = $uid;
        $userlog = model("User")->userlog($data);
        $userlogB = model("User")->userlogB($data);

    }

    //存储最新的设备id
    public function jgidupadd($uid,$jgid){
        $map['ppid'] = $jgid;
        $where['uid'] = $uid;
        if (M('jgid')->where($where)->find()){
            $data = M('jgid')->where($where)->update($map);
        }else{
            $map['uid'] = $uid;
            $data = M('jgid')->insert($map);
        }
    }

    //登陆成功session存储
    private function sessionUpdate($phone,$userid,$username){
        session('username',$username);
        session('userid',$userid);
        session('number',$phone);
    }
}